package io.renren.modules.eightapp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.eightapp.dto.EiCostInfoDTO;
import io.renren.modules.eightapp.dto.EiPayInfoDTO;
import io.renren.modules.eightapp.excel.EiPayInfoExcel;
import io.renren.modules.eightapp.service.EiPayInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@RestController
@RequestMapping("eightapp/eipayinfo")
@Api(tags="")
public class EiPayInfoController {
    @Autowired
    private EiPayInfoService eiPayInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("eightapp:eipayinfo:page")
    public Result<PageData<EiPayInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<EiPayInfoDTO> page = eiPayInfoService.page(params);

        return new Result<PageData<EiPayInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("eightapp:eipayinfo:info")
    public Result<EiPayInfoDTO> get(@PathVariable("id") Long id){
        EiPayInfoDTO data = eiPayInfoService.get(id);

        return new Result<EiPayInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("eightapp:eipayinfo:save")
    public Result save(@RequestBody EiPayInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        eiPayInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("eightapp:eipayinfo:update")
    public Result update(@RequestBody EiPayInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        Date date = new Date();
        dto.setUpdateTime(date);
        eiPayInfoService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("eightapp:eipayinfo:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        eiPayInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("eightapp:eipayinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<EiPayInfoDTO> list = eiPayInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, EiPayInfoExcel.class);
    }

    @GetMapping("getPayInfoAllDataList")
    @ApiOperation("PayInfo表，获取所有订单")
    @LogOperation("PayInfo表，获取所有订单")
    public Result<List<EiPayInfoDTO>> getPayInfoAllDataList() {
        List<EiPayInfoDTO> allDataList = eiPayInfoService.getPayInfoAllDataList();
        return new Result<List<EiPayInfoDTO>>().ok(allDataList);
    }

    @GetMapping("getPayInfoDataListByOwnerId/{ownerId}")
    @ApiOperation("PayInfo表，根据业主ID获取订单")
    @LogOperation("PayInfo表，根据业主ID获取订单")
    public Result<List<EiPayInfoDTO>> getPayInfoDataListByOwnerId(@PathVariable("ownerId") Integer ownerId) {
        List<EiPayInfoDTO> listByOwnerId = eiPayInfoService.getPayInfoDataListByOwnerId(ownerId);
        return new Result<List<EiPayInfoDTO>>().ok(listByOwnerId);
    }

    @GetMapping("getPayInfoDataListByMemo")
    @ApiOperation("PayInfo表，根据费用类型获取订单")
    @LogOperation("PayInfo表，根据费用类型获取订单")
    public Result<List<EiPayInfoDTO>> getPayInfoDataListByMemo(@Param("memo") String memo){
        List<EiPayInfoDTO> listByMemo = eiPayInfoService.getPayInfoDataListByMemo(memo);
        return new Result<List<EiPayInfoDTO>>().ok(listByMemo);
    }

    @GetMapping("getPayInfoTotalByYear")
    @ApiOperation("PayInfo表，根据年份获取本年度总收益")
    @LogOperation("PayInfo表，根据年份获取本年度总收益")
    public Result<Integer> getPayInfoTotalByYear(@Param("year") String year){
        Integer total = eiPayInfoService.getPayInfoTotalByYear(year);
        return new Result<Integer>().ok(total);
    }

}